看了几道变量提升的小题想分享给大家,都很简单,我这里就不解释原因了。
-
我们先说一下预编译的过程
- 创建AO对象
- 找形参和变量声明,放入AO中,值为undefined
- 形参实参相统一
- 找函数声明,给AO中相应的的对象赋函数体
- 下面我们来看几个题:
1.
console.log(bar());//11 打印的是函数的返回值
function bar(){
foo = 100;
function foo(){}
var foo = 11;
return foo;
}
1. AO {
}
2. AO {
foo: undefined //变量声明
}
3. AO {
foo: undefined //无形参
}
4. AO {
foo: function foo(){}
}
2.
function bar(){
return foo;
foo = 100;
function foo(){}
var foo = 11;
}
console.log(bar());//fun
1. AO {
}
2. AO {
foo: undefined //变量声明
}
3. AO {
foo: undefined //无形参
}
4. AO {
foo: function foo(){}
}
3.
function test(){
console.log(b);//unde
if(a){
var b = 100;
}
c = 234;
console.log(c);//234
}
var a;
test();
a = 10;
console.log(c);//234
console.log(global);//unde
global = 100;
console.log(global);//100
function fn(){
console.log(global);//unde
global = 200;
console.log(global);//200
var global = 300;
console.log(global) //300
}
fn();
var global;
console.log(test);//fun
4.
function test(test){
console.log(test);//fun
var test = 234;
console.log(test);//234
function test(){}
}
test(1);
var test = 123;
console.log(test);//123
5.
function test(a,b){
console.log(a);//fun
console.log(b);//unde
var b = 234;
console.log(b);//234
a = 123;
console.log(a);//123
function a(){}
var a;
b = 28;
var b = function(){}
console.log(a);//123
console.log(b);//fn
}
test(1);
6.
function test(a,b){
console.log(a);//1
c = 0;
var c;
a = 3;
b = 2;
console.log(b);//2
function b(){}
function d(){}
console.log(b);//2
}
test(1);
7.
function fn(a){
console.log(a);//fun
var a = 123;
console.log(a);//123
function a(){}
console.log(a);//123
var b = function(){}
console.log(b);//fun
function d(){};
}
fn(1);
8.
console.log(a);//fun
a();//fun
var a = 3;
function a(){
console.log(a);
}
console.log(a);//3
a = 6;
a();//error a 不是一个函数 a = 6
9.
a(3);
var a = 1;
function a(a) {
console.log(a); //3
console.log(b); //fun
b = 1;
c = 2;
console.log(b); //1
var b = 5;
console.log(d); //fun
function b(){};
var a = 6;
console.log(c); // 2
if(false){
var d = 1;
c = 5;
}
console.log(c); //2
function d(){};
console.log(d); //fun
arguments[0] = 7;
console.log(a); //7
}
console.log(a); // 1
console.log(c); // 2
10.
function Foo(){
getName = function(){
alert(1);
};
return this;
}
Foo.getName = function(){alert(2);};
Foo.prototype.getName = function(){alert(3);};
var getName = function(){alert(4);};
function getName(){alert(5);};
Foo.getName();//2
getName();//4
Foo().getName();//1
new Foo.getName();//2
new Foo().getName();//3
new new Foo().getName();//3
其实上面一道题有关于变量提升但是不是重点因为还有原型、继承、运算符优先级等知识点。
-
闲扯
- parseInt
console.log(parseInt('a', 16)); //10 console.log(parseInt(function(){}, 16)); //15 'f' console.log(parseInt(parseInt, 16)); //15 'f' console.log(parseInt(0.000002)); //0 console.log(parseInt(0.0000002)); //2 '2e-7' 大于等于7位才会用科学计数法表示 console.log(parseInt(false, 16)); //250 'fa' console.log(parseInt("113", 2)); //3 '11'
- 立即执行函数的写法
(function a() { console.log(1); })(); (function a() { console.log(1); }()); +function a() { console.log(1); }(); -function a() { console.log(1); }(); !function a() { console.log(1); }(); false || function a() { console.log(1); }(); true && function a() { console.log(1); }(); var a = function a() { console.log(1); }();
上面的代码都相当于是立即执行函数,其实主要是你把它变成一个表达式它就能立即执行。也就是**只有表达式才能被执行符号执行**。
+ 两个数组相加
```
let a = [1, 2],
b = [5, 3];
console.log(a + b); //1,25,3 相当于调用了toString()
```
+ ==相等比较
我们都知道```+0、-0、false、null、undefined、""、NaN```都是假值。
我们来看几个例子:
```
console.log(a + b); //1,25,3
console.log(false == ""); //true
console.log(false == []); //true
console.log(false == 0); //true
console.log(false == null); //false
console.log(false == undefined); //false
console.log(false == NaN); //false
console.log(false == {}); //false
```
```
console.log("" == []); //true
console.log("" == 0); //true
console.log("" == null); //false
console.log("" == undefined); //false
console.log("" == NaN); //false
console.log("" == {}); //false
```
```
console.log(0 == ""); //true
console.log(0 == []); //true
console.log(0 == null); //false
console.log(0 == undefined); //false
console.log(0 == NaN); //false
console.log(0 == {}); //false
```
还有几个比较难以理解的例子:
```
console.log([] == ![]); //true 因为![] = false, false == []是成立的。
2 == [1] //true
"" == [null] //true
0 == "/n" //true 因为'/n'被转换为0
```
+ [] + {}的问题
```
console.log([] + {}); // "[object Object]" [] = "", "" + {} 调用{}的toString()
console.log({} + []); // 0
只不过是转换的前后顺序不同
```
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。